<?php
namespace app\index\controller;

use app\BaseController;
use app\common\model\mysql\CarUser as CarUserModel;
use app\common\model\mysql\CarUserSalary;
use think\facade\View;

class CarUser extends BaseController
{
    public function getCaruserList(){
        $carUserModel = new CarUserModel();
        $page = $this->request->param('page',1,'intval');
        $pageSize = $this->request->param('limit',10,'intval');
        $status = $this->request->param('status',-1,'intval');
        $where = [];
        if($status){
            $where['state'] = $status;
        }
        $data = $carUserModel->getCarUserByPage($page,$pageSize,$where);
        return pageShow(config('status.code.success'),'OK', $data, $carUserModel->getTotal($where), 200);
    }

    public function addCarUser(){
        if($this->request->isPost()){
            $data = $this->request->param(); 
            unset($data['id']);
            if(empty($data['resignation_time'])){
                unset($data['resignation_time']);
            }else{
                $data['state'] = 2;
            }

            $data['share_ratio'] = $data['share_ratio'] * 100;
            $data['create_time'] = time();
            $data['update_time'] = time();
            $carUserModel = new CarUserModel();
            $result = $carUserModel->insertCarUser($data);
            if($result){
                return show(config('status.success'),'添加成功');
            }else{
                return show(config('status.error'),'添加失败,内部异常');
            }
        }
        return show(config('status.error'),'添加失败');
    }

    //修改人员信息
    public function editCarUser(){
        $carUserModel = new CarUserModel();
        if($this->request->isPost()){
            $data =  $this->request->param();
            $id = $data['id'];
            unset($data['id']);
            $data['update_time'] = time();
            if(empty($data['resignation_time'])){
                $data['resignation_time'] = null;
                $data['state'] = 1;
            }else{
                $data['state'] = 2;
            }
            $data['share_ratio'] = $data['share_ratio'] * 100;
            $result = $carUserModel->updateById($id,$data);
            if($result){
                return show(config('status.success'),'修改成功');
            }else{
                return show(config('status.error'),'修改失败');
            }
        }
        $id = $this->request->param("id",0,'intval');
        if(empty($id)){
            return show(config('status.error'), '请求参数错误');
        }
        $result = $carUserModel->getCarUserById($id);
        if($result){
            return show(config('status.success'),'OK',$result);
        }else{
            return show(config('status.error'),'请求失败');
        }
    }

    public function getCaruserCount()
    {
        $carUserModel = new CarUserModel();
        $where = [];
        $status = $this->request->param('status',-1,'intval');
        if($status){
            $where['state'] = $status;
        }
        return pageShow(config('status.code.success'),'OK', $carUserModel->getTotal($where), 200);
    }



    //删除司机
    public function deleteCarUser(){
        if($this->request->isPost()){
            $id = $this->request->param('id',0,'intval');
            if(empty($id)){
                return show(config('status.code.error'),'参数错误');
            }
            $carUserModel = new CarUserModel();
            $result = $carUserModel->deleteById($id);
            $carUserSalaryModel = new CarUserSalary();
            $carUserSalaryModel->deleteByUserId($id);
            if($result){
                return show(config('status.code.success'),'OK');
            }
            return show(config('status.code.error'),'删除失败');
        }
    }


    public function getCarUserByEchart(){
        $timeInterval = $this->request->param('time_interval',date('Y-m-d'),'string');
        $timeDate = explode("~", $timeInterval);
        $where = [];
        if(!empty($timeDate)){
            $where = [
                'enter_date' => [$timeDate[0],$timeDate[1]]
            ];
        }
        $carUserModel = new CarUserModel();
        $carUserSalary = new CarUserSalary();

        $priceCount = 0;
        $carPriceCount = 0;
        $list = $carUserSalary->getCarUserDataByPage($where);
        $priceData = [];
        $allPriceData = [];
        $user = [];
        $userList = $carUserModel->getCarUserList();
        foreach ($userList as $v){
            $user[] = $v->name;
            $priceData[$v->id] = 0;
            $allPriceData[$v->id] = 0;
        }
        foreach ($list as $k => $v){
            $priceCount += $v->price;
            $carPriceCount += $v->total_price;
            $priceData[$v->user_id] += $v->price;
            $allPriceData[$v->user_id] += $v->total_price;
        }
        $data['car_user'] = $user;
        $data['car_user_data'] = array_values($allPriceData);
        $data['priceCount'] = $priceCount;
        $data['carPriceCount'] = $carPriceCount;
        $data['priceData'] = array_values($priceData);
        return show(config('status.code.success'),'OK',$data);
    }
}